package com.blb.utils;


import com.blb.entity.User;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.apache.shiro.util.ByteSource;

public class ShiroUtils {

    /**
     * 获取公司id
     * @return
     */
    public static String getCompany(){
        User user = (User) SecurityUtils.getSubject().getPrincipal();
        return user.getCompanyId();
    }

    /**
     * 获取员信息
     * @return
     */
    public static User getUser(){
        User user = (User) SecurityUtils.getSubject().getPrincipal();
        return user;
    }

    /**
     * 密码加密
     * @param user
     */
    public static void getPass(User user){
        String salt = UUIDUtils.getId();
        String hashAlgorithmName = "MD5";
        String credentials = user.getUserPassword();
        int hashIterations = 1024;
        ByteSource credentialsSalt = ByteSource.Util.bytes(salt);

        Object obj = new SimpleHash(hashAlgorithmName, credentials, credentialsSalt, hashIterations);

        user.setSalt(salt);
        user.setUserPassword(obj.toString());
//        System.out.println("加密后"+user.getPassword());
//        System.out.println("加密后"+user.getSalt());

    }

    public static void main(String[] args) {
        String hashAlgorithmName = "MD5";
        String credentials = "10086";
        int hashIterations = 1024;
        ByteSource credentialsSalt = ByteSource.Util.bytes("d26e2ec5202c412283495589e9189b31");
        /**
         * 参数1：算法名称
         * 参数2：密码
         * 参数3：加密值
         * 参数4：加密次数
         */
        Object obj = new SimpleHash(hashAlgorithmName, credentials, credentialsSalt, hashIterations);
        System.out.println(obj.toString());
    }
}
